package com.desmart.renjiao.employeeself;

import android.text.TextUtils;
import android.util.Log;

import com.desmart.renjiao.App;
import com.desmart.renjiao.bean.BaseResponse;
import com.desmart.renjiao.xmlparase.ResultXmlUtils;
import com.google.gson.Gson;
import com.lemon.library.net.rx.RxObservableListener;
import com.lemon.library.utils.ToastUtils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class EmployeeSelfP extends EmployeeSelf.EmployeeSelfP{

    @Override
    void getEmployeesPort(int title) {

        String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.destram.cn.com/\"><soapenv:Header/><soapenv:Body><ser:GetSsoName><arg0>" +
                App.getUserName() +
                "</arg0></ser:GetSsoName></soapenv:Body></soapenv:Envelope>";
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), xml);
        getRxManager().addObserver(mModel.employeesPort(body), new RxObservableListener<ResponseBody>(mView) {
            @Override
            public void onNext(ResponseBody responseBody) {
                super.onNext(responseBody);

                String ss = null;
                try {
                    ss = responseBody.string();
                    InputStream inputStream = new ByteArrayInputStream(ss.getBytes("UTF-8"));
                    String result = ResultXmlUtils.readXML(inputStream);
                    BaseResponse baseResponse = new Gson().fromJson(result, BaseResponse.class);
                    if ("200".equals(baseResponse.getCode())){
                        mView.onSuccessGetKey(baseResponse.getMassage(),title);
                    }else{
                        ToastUtils.showGravity(baseResponse.getMassage());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

    }
}
